<?php

namespace app\library;

/**
 * 数据处理类
 * Class Data
 * @package app\library
 */
class Data {

    /**
     * 按数据键值分组
     * @param array $data 数据
     * @param string $key 键值
     * @return array
     */
    public static function groupByKey($data, $key) 
    {
        $arr = [];
        foreach ($data as $val) {
            $arr[$val[$key]][] = $val;
        }
        return $arr;
    }
    
    /**
     * 按数据日期分组
     * @param array $data 数据
     * @param string $key 键值
     * @param string $format 日期格式
     * @return array
     */
    public static function groupByDate($data, $key = 'create_time', $format = 'Y年m月d日') 
    {
        $arr = $date_arr = [];
        foreach ($data as $value) {
            $date = date('Y') == date('Y', $value[$key]) ? date(mb_substr($format, 2), $value[$key]) : date($format, $value[$key]);
            $date_arr[$date][] = $value;
        }
        $i = 0;
        foreach ($date_arr as $key => $value) {
            $arr[$i]['date'] = $key;
            $arr[$i]['list'] = $value;
            $i++;
        }
        return $arr;
    }
    
    /**
     * 按数据年份分组
     * @param array $data 数据
     * @param string $key 键值
     * @param string $format 日期格式
     * @return array
     */
    public static function groupByYear($data, $key = 'create_time', $format = 'Y年') 
    {
        $arr = $date_arr = [];
        foreach ($data as $value) {
            $date = date('Ymd') == date('Ymd', $value[$key]) ? date('今天', $value[$key]) : date($format, $value[$key]);
            $date_arr[$date][] = $value;
        }
        $i = 0;
        foreach ($date_arr as $key => $value) {
            $arr[$i]['date'] = $key;
            $arr[$i]['list'] = $value;
            $i++;
        }
        return $arr;
    }
    
    /**
     * 按数据字母分组
     * @param array $data 数据
     * @param string $key 键值
     * @param string $field 字段
     * @return array
     */
    public static function groupByAlpha($data, $key = 'alpha', $field = 'city_name')
    {
        foreach ($data as $k => $val) {
            $data[$k][$key] = strtoupper(substr(\app\library\Pinyin::get($val[$field]), 0, 1));
        }
        return self::groupByKey($data, $key);
    }
    
    /**
     * 替换
     * @global array $send_array
     * @param string $content 数据
     * @param array $params 数据替换
     * @return boolean
     */
    public static function parseContent($content, $params = []) 
    {
        if (empty($content)) {
            return false;
        }
        global $send_array;
        $send_array = $params;
        $temp = preg_replace_callback('/\{\{(\w+)\}\}/i', function ($m) {
            global $send_array;
            return isset($send_array[$m[1]]) ? $send_array[$m[1]] : '';
        }, $content);
        return $temp;
    }
    
    /**
     * hex数据BBC异或校验(两两比较)
     * @param string $byte1
     * @param string $byte2
     * @return string
     */
    public static function hexOr($byte1, $byte2) {
        $result = '';
        $byte1 = str_pad(base_convert($byte1, 16, 2), '8', '0', STR_PAD_LEFT);
        $byte2 = str_pad(base_convert($byte2, 16, 2), '8', '0', STR_PAD_LEFT);
        $len1 = strlen($byte1);
        for ($i = 0; $i < $len1; $i++) {
            $result .= $byte1[$i] == $byte2[$i] ? '0' : '1';
        }
        return strtoupper(base_convert($result, 2, 16));
    }

    /**
     * hex数据BBC异或校验(多个比较)
     * @param string|array $data hex数据
     * @return string
     */
    public static function hexOrArr($data) {
        $result = $data[0];
        for ($i = 0; $i < count($data) - 1; $i++) {
            $result = self::hexOr($result, $data[$i + 1]);
        }
        return $result != '0' ? $result : '00';
    }

}
